#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @Project:     py_utils
# @File:        u_loader.py
# @Author:      Fan GuiChuan
# @Date:        2024/8/20 14:23
# @Version:     Python3.7
# @Description:
import logging
from .u_module import ModuleManager


class PluginLoader(object):
    """
    插件加载器
    """
    def __init__(self, plugin_dir: str, logger: logging.Logger, plugin_entry_file: str = "main"):
        self.logger = logger
        self.plugin_dir = plugin_dir
        self.plugin_entry_file = plugin_entry_file

    def get_valid_plugins(self):
        """
        获取可用插件
        """
        plugin_info = ModuleManager.get_class_map(self.plugin_dir, self.plugin_entry_file)

        return plugin_info

    def load_plugins(self):
        """
        加载所有插件模块类，返回字典
        """
        plugin_info = self.get_valid_plugins()
        ret = {}
        for cls_name, file in plugin_info.items():
            cls_obj = ModuleManager.import_method_from_file(cls_name, file)
            ret[cls_name] = {
                "cls": cls_obj,
                "file_path": file
            }
        return ret
